package com.mfnote.db.dao;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.mfnote.db.DBHelper;
import com.mfnote.db.DBInfo;
import com.mfnote.model.HandEntity;

/**
 * Hand模块数据库操作类
 * 
 * @author ZHF
 * 
 */
public class HandDao {
	public SQLiteDatabase db; // 系统提供的数据库
	private DBHelper dbHelper = null;

	public HandDao(Context context) {
		dbHelper = DBHelper.getInstance(context);// 用static方法来实例化该对象
	}

	/** 打开数据库 **/
	private void openDatabase() {
		db = dbHelper.getWritableDatabase(); // 这里根据数据库名和版本号判断，第一次就调用onCreate创建表，第二次若两者相同就直接返回，若不同调用onUpgrade
	}
	

	/** 插入手写数据 **/
	public long insertHand(HandEntity handEntity) {
		// 打开数据库
		openDatabase();

		ContentValues values = new ContentValues();

		values.put("title", handEntity.getTitle());
		values.put("time", handEntity.getTime());
		values.put("imagePath", handEntity.getImagePath());
		values.put("flag", handEntity.getFlag());

		long raw = db.insert(DBInfo.Table.TB_HAND_NAME, null, values);
		
		db.close();
		return raw; //返回的是当前受影响的行号( 不为-1即为插入成功)
	}

	/** 查询手写表的所有记录 **/
	public ArrayList<HashMap<String, Object>> getHandAllCount() {
		// 打开数据库
		openDatabase();

		ArrayList<HashMap<String, Object>> data = null; // 接收数据

		// Cursor----ResultSet:用于接收数据库数据列表
		Cursor cursor = db.query(DBInfo.Table.TB_HAND_NAME, null, null, null,null, null, null);

		if (null != cursor && cursor.getCount() > 0) {
			data = new ArrayList<HashMap<String, Object>>();

			while (cursor.moveToNext()) {
				HashMap<String, Object> map = new HashMap<String, Object>();

				map.put("title",cursor.getString(cursor.getColumnIndex("title")));
				map.put("time",cursor.getString(cursor.getColumnIndex("time")));
				map.put("imagePath",cursor.getString(cursor.getColumnIndex("imagePath")));
                map.put("flag", cursor.getInt(cursor.getColumnIndex("flag")));
				data.add(map);
			}
		} else{
			System.out.println("没有数据！");
		}
		cursor.close();
		db.close(); // 用完记得要关闭哦
		return data;
	}

	/** 更新手写表的记录--根据标题名称 **/
	public long updateHand(HandEntity handEntity) {

		// 打开数据库
		openDatabase();

		ContentValues values = new ContentValues();

		values.put("title", handEntity.getTitle());
		values.put("time", handEntity.getTime());
		values.put("imagePath", handEntity.getImagePath());
		values.put("flag", handEntity.getFlag());

		long raws = db.update(DBInfo.Table.TB_HAND_NAME, values, "title=?",new String[] { handEntity.getTitle() });
		db.close(); // 用完记得要关闭哦
		return raws;  //返回的是当前受影响的行号
	}

	/** 删除手写的记录 **/
	public long deleteHand(HashMap<String, Object> map) {
		// 打开数据库
		openDatabase();

		long raws = db.delete(DBInfo.Table.TB_HAND_NAME, "title=?", new String[] { map.get("title").toString() });

		db.close();
		return raws; //返回的是当前受影响的行号number of rows affected 
	}
	
	/**根据title判断数据库中是否已经有**/
	public long queryHand(String title){
		long state = -1;
		// 打开数据库
	    openDatabase();
	    Cursor cursor = db.query(DBInfo.Table.TB_HAND_NAME, null, "title=?", new String[] {title}, null, null, null);
		if(cursor!=null&&cursor.getCount()>0){
			state = 1;
		}
	    db.close();
		return state;
	}

	/**查询记录条数**/
	public long queryCount() {
		// 打开数据库
		openDatabase();
				
		String sql = "select count() from "+DBInfo.Table.TB_HAND_NAME;
		SQLiteStatement statement = db.compileStatement(sql);
		long count = statement.simpleQueryForLong();
		return count;
	}
	
	/**根据名称进行查询显示数据
	 *  select * from write where title = name ;**/
	public ArrayList<HashMap<String, Object>> queryByTitle(String name) {
		// 打开数据库
		openDatabase();

		ArrayList<HashMap<String, Object>> data = null; // 接收数据

		// Cursor----ResultSet:用于接收数据库数据列表
		Cursor cursor = db.query(DBInfo.Table.TB_HAND_NAME, null, "title=?", new String[]{name},null, null, null);

		if (null != cursor && cursor.getCount() > 0) {
			data = new ArrayList<HashMap<String, Object>>();

			while (cursor.moveToNext()) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				//title, imagePath,time,flag

				map.put("title",cursor.getString(cursor.getColumnIndex("title")));
				map.put("imagePath",cursor.getString(cursor.getColumnIndex("imagePath")));
				map.put("time", cursor.getString(cursor.getColumnIndex("time")));
				map.put("flag",cursor.getInt(cursor.getColumnIndex("flag")));

				data.add(map);
			}
		}
		cursor.close();
		db.close(); // 用完记得要关闭哦
		return data;
	}
}
